<template>
  <div class="repair-device">
    <div>
      <label>{{ $t("route.Maintenance_Management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Overhaul_facilities") }}</label>
      </button>
    </div>

    <div>
      <div>
        <el-select
          @change="kindChange"
          style="margin-left: 10px"
          v-model="query.kind"
          :placeholder="$t('route.Please_select_the_type')"
        >
          <el-option v-for="item in kindList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>

        <div class="div-handle-right">
          <el-button v-if="false">{{ $t("route.Add") }}</el-button>
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input v-model="query.search" :placeholder="$t('route.Head_Model')" />
        </div>
      </div>

      <el-table class="table" :data="deviceList" style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="kind" :label="$t('route.kind')" />
        <el-table-column prop="noseCode" :label="$t('route.Head_Model')" />
        <el-table-column prop="problem" :label="$t('route.Maintenance_issues')" />
        <el-table-column prop="nickname" :label="$t('route.Maintainer')" />
        <el-table-column :label="$t('route.Whether_to_purchase_new_parts')">
          <template #default="scope">
            {{ scope.row.needBuy == 0 ? $t("route.no") : $t("route.yes") }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Whether_there_are_damaged_parts')">
          <template #default="scope">
            {{ scope.row.hasPart == 0 ? $t("route.no") : $t("route.yes") }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.operate')">
          <template #default="scope">
            <el-button
              v-if="scope.row.partBatch != 0"
              @click="jumpPage(1, scope.row)"
              type="text"
              >{{ $t("route.View_parts") }}
            </el-button>
            <el-button
              v-if="scope.row.partBatch == 0"
              @click="jumpPage(1, scope.row)"
              type="text"
              >{{ $t("route.Purchase_parts") }}
            </el-button>
            <el-button @click="fixClick(scope.row.id)" type="text">{{
              $t("route.Fixed")
            }}</el-button>
            <el-button
              style="color: #ff0000"
              @click="jumpPage(0, scope.row)"
              type="text"
              >{{ $t("route.Scrap") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import Constant from "../../js/common/constant.js";
import i18n from "../../i18n/index";
export default {
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        search: null,
        kind: null,
      },
      deviceList: [],
      kindList: [i18n.global.t("route.All_categories"), ...Constant.DEVICE_KIND],
    };
  },
  methods: {
    fixClick(deviceId) {
      http.post(
        api.UPDATE_DEVICE_STATE,
        {
          deviceId: deviceId,
          state: 0,
        },
        (data) => {
          this.$message.success(i18n.global.t("route.Operation_succeeded"));
          this.getCapitalDevice();
        }
      );
    },
    jumpPage(type, param1) {
      if (type == 0) {
        this.$router.push({
          name: "ADD_SCRAP_DEVICE",
          params: {
            deviceId: param1.id,
            repairId: param1.repairId,
          },
        });
      } else if (type == 1) {
        this.$router.push({
          name: "buy-part",
          params: {
            capitalDevice: JSON.stringify(param1),
          },
        });
      }
    },
    getCapitalDevice() {
      http.post(
        api.GET_CAPITAL_DEVICE,
        {
          adminId: this.$store.state.user.id,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          kind:
            this.query.kind == i18n.global.t("route.All_categories")
              ? null
              : this.query.kind,
          search: this.query.search,
        },
        (data) => {
          this.deviceList = data.data.deviceList;
          this.query.total = data.data.total;
        }
      );
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getCapitalDevice();
    },
    kindChange() {
      this.query.pageNum = 1;
      this.getCapitalDevice();
    },
    searchClick() {
      this.query.pageNum = 1;
      this.getCapitalDevice();
    },

    getAllKind() {
      http.post(
        api.GET_ALL_KIND,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          this.kindList = [i18n.global.t("route.All_categories"), ...data.data];
        }
      );
    },
  },
  created() {
    this.getAllKind();
    this.getCapitalDevice();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.repair-device {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }

    .table {
      margin-top: 20px;
    }
  }

  .select {
    width: 120px;
    margin-top: 10px;
  }

  .echart {
    height: 150px;
    width: 100%;
  }
}
</style>
